/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2021-09-09 10:56:39
 * @LastEditors: lzc
 * @LastEditTime: 2021-09-15 16:25:26
 */
#ifndef _DRV_PACKER_H_
#define _DRV_PACKER_H_

#include <stdint.h>   
#include "stdio.h"
#include "stdlib.h"
#include "string.h" 
#ifdef __cplusplus
extern "C"
{
#endif

//使用动态内存
#define USE_DYNAMIC_MEM 0

#if USE_DYNAMIC_MEM
#define UP_MALLOC
#define UP_FREE
#endif

#define MAX_PACK_SIZE 256 //最长消息长度,最大可用14位即16384

#define STX_HEAD1 0XA5         //数据包头
#define STX_HEAD2 0X5A         //数据包头

#define UPACKER_DEBUG           0
typedef void (*PACKER_CB)(uint16_t command,uint8_t *d, uint16_t s);
typedef void (*SEND_CB)(uint8_t *d, uint16_t s);
typedef struct
{

#if !USE_DYNAMIC_MEM
    uint8_t data[MAX_PACK_SIZE]; //payload的内存
#else
    uint8_t *data; //用来做payload序列化的内存
#endif
    uint16_t flen;  //frame长度

    uint8_t calc;   //frame校验计算值
    uint8_t check;  //frame校验值

    uint8_t state;  //frame解析状态

    uint16_t cnt;   //frame数据接收cnt

    uint8_t version;  //协议版本
    uint16_t command; //frame指令码
    

    PACKER_CB cb;   //数据包处理回调
    SEND_CB send;   //数据发送回调

} upacker_inst;

typedef upacker_inst *upacker_inst_t;

int upacker_init(upacker_inst_t packer, PACKER_CB h, SEND_CB s);
void upacker_pack(upacker_inst_t packer, uint16_t command, uint8_t *buff, uint16_t size);
void upacker_unpack(upacker_inst_t packer, uint8_t *buff, uint16_t size);

#ifdef __cplusplus
}
#endif
#endif
